﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace DSLOrder
{
    public partial class SysConfig : Form
    {
        public SysConfig()
        {
            InitializeComponent();
        }

        Common Init = new Common();
        
         

        private void SysConfig_Load(object sender, EventArgs e)
        {
            string mssqlConnectionString = Common.base64(ConfigurationManager.ConnectionStrings["mssqlConnectionString"].ToString(), false);

            string TokenUrl = ConfigurationManager.AppSettings["TokenUrl"].ToString();
            string GetOrderUrl = ConfigurationManager.AppSettings["GetOrderUrl"].ToString();
            string SendOrderUrl = ConfigurationManager.AppSettings["SendOrderUrl"].ToString();
            string uid = ConfigurationManager.AppSettings["uid"].ToString();
            string user = ConfigurationManager.AppSettings["user"].ToString();
            string pass = ConfigurationManager.AppSettings["pass"].ToString();
            string pricex = ConfigurationManager.AppSettings["pricex"].ToString();

            txtConnectionString.Text = mssqlConnectionString;
            txtTokenUrl.Text = TokenUrl;
            txtGetOrderUrl.Text = GetOrderUrl;
            txtSendOrderUrl.Text = SendOrderUrl;   
            txtpass.Text = pass;
            txtuid.Text = uid;
            txtuser.Text = user;
            txtpricex.Text = pricex;

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void editconnectionStrings()
        {
            txtConnectionString.UseSystemPasswordChar = false;
            txtConnectionString.ReadOnly = false;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateConnectionStringsConfig("mssqlConnectionString", Common.base64(txtConnectionString.Text, true), "ERP数据库地址");

                UpdateappSettingsConfig("TokenUrl", txtTokenUrl.Text);
                UpdateappSettingsConfig("GetOrderUrl", txtGetOrderUrl.Text);
                UpdateappSettingsConfig("SendOrderUrl", txtSendOrderUrl.Text);
                UpdateappSettingsConfig("pass", txtpass.Text);
                UpdateappSettingsConfig("uid", txtuid.Text);
                UpdateappSettingsConfig("user", txtuser.Text);
                decimal pricen=decimal.Parse(txtpricex.Text);
                if (pricen < 1) { pricen = 1; }
                UpdateappSettingsConfig("pricex", pricen.ToString());
                MessageBox.Show("保存成功！\r\n请重新打开软件");
                Application.Exit();
            }
            catch (Exception ex)
            {
                MessageBox.Show("出错了！");
                Init.WriteLog(ex.ToString(), true);
            }
        }
        ///<summary> 
        ///更新连接字符串  
        ///</summary> 
        ///<param name="newName">连接字符串名称</param> 
        ///<param name="newConString">连接字符串内容</param> 
        ///<param name="newProviderName">数据提供程序名称</param> 
        public static void UpdateConnectionStringsConfig(string newName, string newConString, string newProviderName)
        {
            //指定config文件读取
            string file = System.Windows.Forms.Application.ExecutablePath;
            Configuration config = ConfigurationManager.OpenExeConfiguration(file);

            bool exist = false; //记录该连接串是否已经存在  
            //如果要更改的连接串已经存在  
            if (config.ConnectionStrings.ConnectionStrings[newName] != null)
            {
                exist = true;
            }
            // 如果连接串已存在，首先删除它  
            if (exist)
            {
                config.ConnectionStrings.ConnectionStrings.Remove(newName);
            }
            //新建一个连接字符串实例  
            ConnectionStringSettings mySettings =
                new ConnectionStringSettings(newName, newConString, newProviderName);
            // 将新的连接串添加到配置文件中.  
            config.ConnectionStrings.ConnectionStrings.Add(mySettings);
            // 保存对配置文件所作的更改  
            config.Save(ConfigurationSaveMode.Modified);
            // 强制重新载入配置文件的ConnectionStrings配置节  
            ConfigurationManager.RefreshSection("connectionStrings");
        }
        ///<summary> 
        ///更新连接字符串  
        ///</summary> 
        ///<param name="newName">连接字符串名称</param> 
        ///<param name="newConString">连接字符串内容</param> 
        ///<param name="newProviderName">数据提供程序名称</param> 
        public static void UpdateappSettingsConfig(string newKey, string newValue)
        {
            //指定config文件读取
            string file = System.Windows.Forms.Application.ExecutablePath;
            Configuration config = ConfigurationManager.OpenExeConfiguration(file);

            bool exist = false; //记录该连接串是否已经存在  
            //如果要更改的连接串已经存在  

            if (config.AppSettings.Settings[newKey] != null)
            {
                exist = true;
            }
            // 如果连接串已存在，首先删除它  
            if (exist)
            {
                config.AppSettings.Settings.Remove(newKey);
            }
            //新建一个连接字符串实例  
            KeyValueConfigurationElement mySettings =
                new KeyValueConfigurationElement(newKey, newValue);
            // 将新的连接串添加到配置文件中.  
            config.AppSettings.Settings.Add(mySettings);
            // 保存对配置文件所作的更改  
            config.Save(ConfigurationSaveMode.Modified);
            // 强制重新载入配置文件的ConnectionStrings配置节  
            ConfigurationManager.RefreshSection("AppSettings");
        }

        private void txtpricex_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '.' && this.txtpricex.Text.IndexOf(".") != -1)
            {
                e.Handled = true;
            }

            if (!((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == '.' || e.KeyChar == 8))
            {
                e.Handled = true;
            }  
        }

        int mci = 0;

        private void txtConnectionString_MouseClick(object sender, MouseEventArgs e)
        {
            mci += 1;
            if (mci == 5)
            {
                editconnectionStrings();
            }
        }
    }
}
